package com.yanwq.sayhi.controller.comment.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yanwq.sayhi.R;
import com.yanwq.sayhi.common.client.MImageLoader;
import com.yanwq.sayhi.common.tool.DateFormatTool;
import com.yanwq.sayhi.controller.comment.activity.CommentListActivity;
import com.yanwq.sayhi.dao.bean.Comment;
import com.yanwq.sayhi.dao.bean.Reply;
import com.yanwq.sayhi.widget.FixedListView;
import com.yanwq.sayhi.widget.InputDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 作者：yanweiqiang。
 * 日期：2016/2/24 12:24。
 */
public class CommentAdapter extends BaseAdapter {

    private List<Comment> mList;

    public CommentAdapter() {
        mList = new ArrayList<>();
    }

    public void refresh(List<Comment> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void load(List<Comment> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Context context = parent.getContext();
        ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_comment,
                    parent,
                    false);
            holder.item = convertView.findViewById(R.id.comment_item);
            holder.avatar = (ImageView) convertView.findViewById(R.id.comment_avatar);
            holder.nickname = (TextView) convertView.findViewById(R.id.comment_nickname);
            holder.time = (TextView) convertView.findViewById(R.id.comment_time);
            holder.content = (TextView) convertView.findViewById(R.id.comment_content);
            holder.sofa = convertView.findViewById(R.id.comment_sofa);
            holder.replies = (FixedListView) convertView.findViewById(R.id.comment_replies);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Comment comment = mList.get(position);

        MImageLoader.displayImage(comment.getUser().getAvatarThumbUrl(), holder.avatar);
        holder.nickname.setText(comment.getUser().getNickname());
        holder.time.setText(DateFormatTool.format(comment.getPublishTime()));
        holder.content.setText(comment.getContent());

        if (comment.getReplies() != null && comment.getReplies().length > 0) {
            holder.sofa.setVisibility(View.GONE);
        } else {
            holder.sofa.setVisibility(View.VISIBLE);
        }

        displayReplies(holder.replies, Arrays.asList(comment.getReplies()), comment);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final InputDialog inputDialog = new InputDialog(context);
                inputDialog.setPrompt(comment.getContent());
                inputDialog.setOk(context.getString(R.string.send));
                inputDialog.setCancel(context.getString(R.string.forget_it));
                inputDialog.setOkListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((CommentListActivity) context).sendReply(inputDialog.getContent(), comment, comment.getUser());
                    }
                });
                inputDialog.show();
            }
        });
        return convertView;
    }

    private void displayReplies(ListView listView, List<Reply> replies, Comment comment) {
        ReplyAdapter adapter = new ReplyAdapter(replies, comment);
        listView.setAdapter(adapter);
    }

    private class ViewHolder {
        View item;
        ImageView avatar;
        TextView nickname;
        TextView time;
        TextView content;
        View sofa;
        FixedListView replies;
    }
}
